﻿Imports System.Data.SqlClient
Public Class _Default
    Inherits Page

    'Protected Sub Page_PreInit(sender As Object, e As EventArgs)
    '    If Request.Browser.IsMobileDevice Then
    '        MasterPageFile = "~/Site.Mobile.Master"
    '    End If
    'End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Request.Browser.IsMobileDevice Then
            Response.Redirect("~/Default_mobile.aspx")
        End If

        Dim rn As New Random
        Dim n As Integer = rn.Next(1, 6)
        getRandomPictures(n)

        'Count Activity
        If IsPostBack = False Then
            Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
            Dim SqlString As String = "INSERT INTO Site_Activity(Activity) VALUES (@f1)"
            Dim cmd As SqlCommand = New SqlCommand(SqlString, conn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", "Home")
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
        End If
    End Sub

    Protected Sub getRandomPictures(ByVal r As Integer)
        'Declaring an empty sqlstring
        Dim sqlString As String = ""

        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)

        'Creating a switch in order to choose a sqlstring based on the random number
        Select Case (r)
            Case Is = 1
                sqlString = "SELECT TOP 3 ID, BannerPicture FROM Books WHERE IsVerified = 1 ORDER BY ID DESC"
                lbl_Banner.Text = "Most recent additions"
            Case Is = 2
                sqlString = "SELECT TOP 3 AverageRating, BannerPicture FROM Books_With_Average_Rating WHERE IsVerified = 1 ORDER BY AverageRating DESC"
                lbl_Banner.Text = "Highest rated books"
            Case Is = 3
                sqlString = "SELECT TOP 3 TotalLikes, BannerPicture FROM Books_With_Most_Likes WHERE IsVerified = 1 ORDER BY TotalLikes DESC"
                lbl_Banner.Text = "Most liked Books"
            Case Is = 4
                sqlString = "SELECT TOP 3 TotalComments, BannerPicture FROM Books_With_Most_Comments WHERE IsVerified = 1 ORDER BY TotalComments DESC"
                lbl_Banner.Text = "Most discussed Books"
            Case Is = 5
                sqlString = "SELECT TOP 3 TotalViews, BannerPicture FROM Books_With_Most_Views WHERE IsVerified = 1 ORDER BY TotalViews DESC"
                lbl_Banner.Text = "Most viewed Books"
        End Select

        Dim cmd As SqlCommand = New SqlCommand(sqlString, conn)
        cmd.CommandType = CommandType.Text
        conn.Open()
        Dim dr As SqlDataReader = cmd.ExecuteReader()
        Dim myList As New List(Of String)

        While dr.Read()
            myList.Add(dr("BannerPicture"))
        End While
        'If list has less than 3 beaches to show, then a black picture is assigned to banner
        If myList.Count < 3 Then
            Image1.ImageUrl = "Images/blank.png"
            Image2.ImageUrl = "Images/blank.png"
            Image3.ImageUrl = "Images/blank.png"
            lbl_Banner.Text = "No Books found"
        Else
            Image1.ImageUrl = myList(0)
            Image2.ImageUrl = myList(1)
            Image3.ImageUrl = myList(2)
        End If
        conn.Close()
    End Sub
End Class